import datetime
import sys
import time
import traceback
import static.globals as sg
from cache.redis_util import getConnection


def getTodayStr():
    today = datetime.date.today().strftime("%Y-%m-%d")
    return today

def getNowTime():
    nowTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    return nowTime


def isStockOpen():
    today = datetime.date.today().strftime("%Y-%m-%d")
    # 开盘时间
    morningBegin = datetime.datetime.strptime(f'{today} 01:15:00', "%Y-%m-%d %H:%M:%S")
    morningEnd = datetime.datetime.strptime(f'{today} 11:30:00', "%Y-%m-%d %H:%M:%S")
    afternoonBegin = datetime.datetime.strptime(f'{today} 13:00:00', "%Y-%m-%d %H:%M:%S")
    afternoonEnd = datetime.datetime.strptime(f'{today} 15:00:00', "%Y-%m-%d %H:%M:%S")
    currentTime = datetime.datetime.today()
    if(currentTime > morningBegin and currentTime< morningEnd):
        return True
    elif(currentTime > afternoonBegin and currentTime< afternoonEnd):
        return True
    else:
        return False

def isOpenToday():
    isOpenToday = True
    redis = None
    try:
        profile = sg.profile
        redis = getConnection(profile)
        if(redis.get("stock:sta:date").decode('utf-8') == '--'):        ## 今天不开盘
            print("==================Stock is not open today==================")
            time.sleep(120)
            isOpenToday = False

    except Exception as e:
        print(f"异常信息: {e}")
        print("详细堆栈信息:")
        traceback.print_exc()  # 打印完整的堆栈跟踪
        print(f'====task exception:{getNowTime()}====')
        if redis != None:
            redis.close()

        time.sleep(2)
        print(f'====exception occur sys quit====')
        sys.exit()


    return isOpenToday



